package com.neiquan.secondhand.exception;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class ExceptionResolver implements HandlerExceptionResolver {
	public final static Logger log = LoggerFactory.getLogger(ExceptionResolver.class);
	
	public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex) {
		response.setStatus(HttpStatus.OK.value());
		response.setContentType(MediaType.APPLICATION_JSON_VALUE);
		try {
			if(!(ex instanceof ResponseException)) {
				response.getWriter().print("{\"code\":"+599+",\"message\":\""+ex.getMessage()+"\"}");
			} else {
				response.getWriter().print(ex.getMessage());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		log.error(ex.toString());
		return new ModelAndView();
	}

}
